Ext.ns('Wlanauth.user');

Wlanauth.user.EditFormPanel = Ext.extend(Wlanauth.BaseFormPanel,{
	title:'用户资料修改',
	frame:false,
	editMode:'UPDATE',
	width:500,
	constructor:function(config){
		Ext.apply(this,config);
	 	
	 	var formID = new Date().getTime();
		
		/*var photoHidden = new Ext.form.Hidden({
			//name:'user.photo'
		});*/
	 	
	 	var compositefield = new Ext.form.CompositeField({
	 		fieldLabel:'所需费用',
			combineErrors: false,
            items: [{
            	xtype: 'displayfield',width:80,value:'1',cls:'x-field-display'
            },{
            	xtype: 'displayfield',value: 'M * '
            },{
            	xtype: 'displayfield',name:'radratio',value:'loading...',width:40,cls:'x-field-display'
            },{
            	xtype: 'displayfield',value:'KB/元  = '
            },{
            	xtype: 'displayfield',readOnly:true,
            	width: 100,allowBlank: true,cls:'x-field-display',value:'  '
            },{
            	xtype: 'displayfield',value: '元'
            }]
	 	});
	 	//基本信息
	 	var commonInfoPanel = new Ext.form.FieldSet({
        	title:'基本资料',
        	layout:'form',
        	defaults:{xtype:'textfield',width:320},
    		items:[{
    			fieldLabel:'绑定IP',name:'entity.bindIP',cls:'x-field-display'
    		},{
    			fieldLabel:'卡号',name:'entity.username',allowBlank:false,maxLength:25,
    			xtype:'keytextfield',
    			url:'user!list.action?dataOnly=true'
    		},{
    			fieldLabel:'PUK码',name:'entity.puk',maxLength:50,allowBlank:false
    		},{
    			fieldLabel:'密码',name:'entity.password',allowBlank:false,xtype:'hidden'
    		},{
    			fieldLabel:'过期时间',name:'entity.expiredTime',xtype:'datefield',format:'Y-m-d',allowBlank:false
    		},
//    		{
//    			fieldLabel:'类型',name:'entity.userType',xtype:'localcombo',data:[['流量卡','DEFAULT']],value:'DEFAULT',allowBlank:false
//    		},
    		{
    			fieldLabel:'总流量(Mb)',name:'entity.maxOctets',allowBlank:false,
    			xtype:'localcombo',data:[['1G卡','1024'],['2G卡','2048'],['3G卡','3072']],editable:true,
    			listeners:{
    				'change':function(field,newVal,oldVal){
    					var items = compositefield.items;
    					items.itemAt(0).setValue(newVal);
    					
    					var radRatioCmp = items.itemAt(2);
    					if(radRatioCmp.inited){
    						var val = Math.round((newVal*radRatioCmp.getValue())*100)/100;
    						items.itemAt(4).setValue(val);
    						//compositefield.nextSibling().setValue(val);
    					}
 
    				}
    			}
    		},
//    		{
//    			fieldLabel:'已用流量',xtype:'displayfield',name:'historyOcts',anchor:'95%'
//    		},
    		//compositefield,
    		{
				fieldLabel:'实收金额(元)',
				xtype:'numberfield',
				allowBlank:false,
				name:'entity.actualFee'
			}]
	 	});
	 	var otherInfoPanel = new Ext.form.FieldSet({
	 		title:'其它信息',
	 		layout:'form',
	 		items:[{
    			fieldLabel:'注册日期',name:'entity.joinTime',xtype:'displayfield',cls:'x-field-display',width:300
    		},{
    			fieldLabel:'激活日期',name:'entity.activeTime',xtype:'displayfield',cls:'x-field-display',value:'    ',width:300
    		},{
    			fieldLabel:'状态',name:'entity.status',xtype:'localcombo',data:[['启用','ENABLED'],['禁用','DISABLED']],allowBlank:false
    		},{
    			xtype:'hidden',
    			name:'entity.id'
    		}]
	 	});
	 	Wlanauth.user.EditFormPanel.superclass.constructor.call(this,{
			actionClass:'user',
			buttonAlign:'center',
			waitMsgTarget: true,
	        padding:5,
	        items:[commonInfoPanel,otherInfoPanel],
	        buttons:[{
	        	text:'修改',
	        	iconCls:'edit',
	        	handler:this.doSubmit,
	        	scope:this
	        },{
	        	text:'取消',
	        	iconCls:'cancel',
	        	handler:function(){
	        		ZZExt.ApplicationContext.removeTabCmp(this);
	        	},
	        	scope:this
	        }]
     	});
	 	
	 	this.on('actionSuccess',function(){
	 		var _this = this;
	 		Ext.Msg.confirm('提示','操作成功,数据已保存! 是否重新修改?',function(btn){
	 			if(btn=='yes'){
	 				//doNothing
	 			} else {
	 				ZZExt.ApplicationContext.removeTabCmp(_this);
	 				var tab =ZZExt.ApplicationContext.openTab('wlanauth.user.tabpanel');
	 				tab.tabPanel.items.itemAt(0).getStore().reload();
	 			}
	 		});
	 	  
	 	},this);
	 	/*Ext.Ajax.request({
	 		async: false, 
	 		url:'systemProperty!findKey.action?systemProperty.key=RADIUS_RATIO',
	 		success:function(response,opts){
	 			var r = Ext.decode(response.responseText);
	 			if(r.success){
	 				var f = this.getForm();
	 				var radRatioCmp = f.findField('radratio');
	 				radRatioCmp.setValue(r.data.value);
	 				radRatioCmp.inited=true;
	 				var field = f.findField('entity.maxOctets');
	 				field.fireEvent('change',field,field.getValue());
	 			}
	 		},
	 		scope:this
	 	});
	 	*/
	 	function renderStatus(form,status){
	 		var desc = '<font style="color:blue">正常状态用户</font>';
	 		if(status == 'EXPIRED'){
	 			desc = '<font style="color:red">过期用户</font>';
	 		}else if(status == 'NONEOCT') {
	 			desc = '<font style="color:red">流量用完用户</font>';
	 		} else if(status == 'DISABLED'){
	 			desc = '<font style="color:red">该用户已禁用</font>';
	 		}
	 		form.findField('status').setValue(desc);
	 	}
	 	this.on('ZZExt.init',function(params){
	 		var id = params.id;
		 	this.loadDataById(id);
	 	});
	},
	isValid:function(form){
		var flag = form.isValid();
		if(!flag) return false;
		var oct = form.findField('entity.maxOctets').getValue();
		var reg = /^\d+$/;
		if(oct==null||!reg.test(oct)||oct<1){
			Ext.Msg.alert('提示','总流量必须为大于或等于1的整型数字');
			flag = false;
		}
		return flag;
	},
	onLoadDataSuccess:function(form,action){
		/*var field = form.findField('entity.maxOctets');
		field.fireEvent('change',field,field.getValue());
		
		var data = action.result.data;
		var sumIn = Math.round(data.sumInputOctets/10.24)/100;
 		var sumOut = Math.round(data.sumOutputOctets/10.24)/100;
 		var _html='上行: '+sumIn;
		_html+=' Kb&nbsp;&nbsp;下行:'+sumOut;
		_html+=' Kb&nbsp;&nbsp;总计: <font style="color:red">'+(sumIn+sumOut)+'</font> Kb';
		
		form.findField('historyOcts').setValue(_html);
		*/
    }
	
});

Ext.reg('wlanauth.user.editformpanel',Wlanauth.user.EditFormPanel);